package mlz.c_dead;

/*
    避免死锁方式二: 加锁顺序
        前提条件: 需要一次性锁住多个资源对象,可以根据资源的某个属性或者hashCode值来定义锁的先后顺序.

 */
public class DeadLockDemo03 {
    public static void main(String[] args) {
        Object objA = new Object();
        Object objB = new Object();

        new Thread(){
            @Override
            public void run() {
                if (objA.hashCode() > objB.hashCode()){
                    synchronized (objA){
                        System.out.println("线程2: 被锁住了");
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        //进入到同步锁B
                        synchronized (objB){
                            System.out.println("拿不到线程1");
                            try {
                                Thread.sleep(4000);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        System.out.println("被锁住了,无法打印~~");
                    }
                }else {
                    synchronized (objB){
                        System.out.println("线程2: 被锁住了");
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        //进入到同步锁B
                        synchronized (objA){
                            System.out.println("拿不到线程1");
                            try {
                                Thread.sleep(4000);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        System.out.println("被锁住了,无法打印~~");
                    }
                }
            }
        };

        new Thread(){
            @Override
            public void run() {
                if (objB.hashCode() > objA.hashCode()){
                    //先进入A锁同步
                    synchronized (objB){
                        System.out.println("线程2: 被锁住了");
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        //进入到同步锁B
                        synchronized (objA){
                            System.out.println("拿不到线程1");
                            try {
                                Thread.sleep(4000);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        System.out.println("被锁住了,无法打印~~");
                    }
                }else {
                    //先进入A锁同步
                    synchronized (objA){
                        System.out.println("线程2: 被锁住了");
                        try {
                            Thread.sleep(3000);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        //进入到同步锁B
                        synchronized (objB){
                            System.out.println("拿不到线程1");
                            try {
                                Thread.sleep(4000);
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                        }
                        System.out.println("被锁住了,无法打印~~");
                    }
                }
            }
        }.start();


    }
}








